package com.example.dangerouscargodriver.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.entry.ResourceInfo;
import com.example.dangerouscargodriver.entry.ResourceStatusInfo;
import com.example.dangerouscargodriver.ui.activity.MyResourceDetailActivity;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class ResourceManagerInfoAdapter extends RecyclerView.Adapter<ResourceManagerInfoAdapter.ViewHolder>{

	private static final String TAG = "ResourceManagerInfoAdapter";
	private List<ResourceStatusInfo> mResourceInfoItemList = null;
	private Context mContext;
	private ClickListener mClickListener;
	private View mRootView;

	static class ViewHolder extends RecyclerView.ViewHolder {
		private TextView mFromName = null;
		private TextView mToName = null;
		private TextView mSourceDistance = null;
		private TextView mPublishTime = null;
		private TextView mPublisher = null;
		private TextView mPublishState = null;
		private TextView mRegTime = null;
		private TextView mLoadTime = null;
		private TextView mLoadTimeTit = null;
		private Button mGrapOrder = null;
		private LinearLayout mPublishTimeLay;
		private LinearLayout mMetaLayout = null;
		private Context mContext = null;

		public ViewHolder(View view) {
			super(view);
			mContext = view.getContext();
			mFromName = view.findViewById(R.id.item_start_city);
			mSourceDistance = view.findViewById(R.id.item_source_distance);
			mToName = view.findViewById(R.id.item_end_city);
			mPublishTime = view.findViewById(R.id.tv_publish_time);
			mPublisher = view.findViewById(R.id.tv_resource_publisher);
			mPublishState = view.findViewById(R.id.tv_order_state);
			mRegTime = view.findViewById(R.id.item_register_time);
			mGrapOrder = view.findViewById(R.id.btn_grab_order);
			mPublishTimeLay = view.findViewById(R.id.lay_publish_time);
			mMetaLayout = view.findViewById(R.id.layout_resource_meta);
			mLoadTime = view.findViewById(R.id.tv_load_time);
			mLoadTimeTit = view.findViewById(R.id.tv_load_time_title);

			mRegTime.setVisibility(View.GONE);
			mGrapOrder.setVisibility(View.GONE);
			mPublishState.setVisibility(View.VISIBLE);
			mPublisher.setVisibility(View.VISIBLE);
			mPublishTimeLay.setVisibility(View.VISIBLE);

			mLoadTimeTit.setTextColor(Color.GRAY);
			mLoadTime.setTextColor(Color.GRAY);
		}

	}

	public ResourceManagerInfoAdapter() {
		mResourceInfoItemList = new ArrayList<>();
		mClickListener = new ClickListener();
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		mRootView = LayoutInflater.from(parent.getContext()).inflate(
				R.layout.layout_resource_info_item, parent, false);
		ViewHolder holder = new ViewHolder(mRootView);

		return holder;
	}

	@SuppressLint("LongLogTag")
	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		final int selectPos = position;
		int stateColor = Color.BLACK;
		String loadTime = "";
		ResourceStatusInfo resourceInfo = mResourceInfoItemList.get(position);
		Log.d(TAG, "resourceInfo: " + resourceInfo.toString());
		holder.mFromName.setText(resourceInfo.getFrom_name());
		holder.mToName.setText(resourceInfo.getTo_name());
		holder.mSourceDistance.setText(resourceInfo.getSource_distance() + "km");
		holder.mPublishTime.setText(resourceInfo.getRegtime());

		if(resourceInfo.getLoadtime2() == null ||resourceInfo.getLoadtime().equals(resourceInfo.getLoadtime2())){
			loadTime = resourceInfo.getLoadtime();
		}
		else{
			loadTime = resourceInfo.getLoadtime() + "~" + resourceInfo.getLoadtime2();
		}

		holder.mLoadTime.setText(loadTime);
		holder.mPublishState.setText(resourceInfo.getStatus_name());

		switch (resourceInfo.getStatus()){
			case "1":
				stateColor = Color.rgb(255,165,0);
				break;
			case "2":
				stateColor = Color.YELLOW;
				break;
			case "3":
				stateColor = Color.BLUE;
				break;
			case "4":
				stateColor = Color.BLACK;
				break;
			case "5":
				stateColor = Color.RED;
				break;
			case "6":
				stateColor = Color.RED;
				break;
		}

		holder.mPublishState.setTextColor(stateColor);
		holder.mPublisher.setText(resourceInfo.getName() + "(" + resourceInfo.getMobilephone() + ")");

		if(resourceInfo.getName() != null && !resourceInfo.getName().equals("")){
			View layout = LayoutInflater.from(holder.mContext).inflate(R.layout.layout_resource_meta_info_item, null);
			TextView textView = layout.findViewById(R.id.tv_item_meta_string);
			textView.setText(resourceInfo.getName());
			holder.mMetaLayout.addView(layout);
		}
		if(resourceInfo.getClassname() != null && !resourceInfo.getClassname().equals("")){
			View layout = LayoutInflater.from(holder.mContext).inflate(R.layout.layout_resource_meta_info_item, null);
			TextView textView = layout.findViewById(R.id.tv_item_meta_string);
			textView.setText(resourceInfo.getClassname());
			holder.mMetaLayout.addView(layout);
		}
		if(resourceInfo.getWeight() != null && !resourceInfo.getWeight().equals("")){
			View layout = LayoutInflater.from(holder.mContext).inflate(R.layout.layout_resource_meta_info_item, null);
			TextView textView = layout.findViewById(R.id.tv_item_meta_string);
			textView.setText(resourceInfo.getWeight() + "吨");
			holder.mMetaLayout.addView(layout);
		}
		if(resourceInfo.getTruck_type() != null && !resourceInfo.getTruck_type().equals("")){
			String truckType = resourceInfo.getTruck_type();
			String[] truckTypeArray = truckType.split(",");

			for(int index = 0; index < truckTypeArray.length; index++){
				View layout = LayoutInflater.from(holder.mContext).inflate(R.layout.layout_resource_meta_info_item, null);
				TextView textView = layout.findViewById(R.id.tv_item_meta_string);
				textView.setText(truckTypeArray[index]);
				holder.mMetaLayout.addView(layout);
			}
		}


		holder.itemView.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				mClickListener.onClick(selectPos);
				notifyDataSetChanged();
			}
		});
	}

	@Override
	public int getItemCount() {
		return mResourceInfoItemList.size();
	}

	public class ClickListener {
		private int mPosition = -1;

		void onClick(int position){
			mPosition = position;
			Intent intent = new Intent(mRootView.getContext(), MyResourceDetailActivity.class);
			intent.putExtra("ItemIndex", mPosition);
			mRootView.getContext().startActivity(intent);
		}

		public int getPosition() {

			return mPosition;
		}

		public void setPosition(int mPosition) {

			this.mPosition = mPosition;
		}
	}

	public void addData(int position, ResourceStatusInfo item) {
		mResourceInfoItemList.add(position, item);
		notifyItemInserted(position);
	}

	public void removeData(int position) {
		mResourceInfoItemList.remove(position);
		notifyItemRemoved(position);
	}

	public void updateData(int position, ResourceStatusInfo item) {
		mResourceInfoItemList.set(position, item);
		notifyItemChanged(position);
	}

	public void clearData(){
		mResourceInfoItemList.clear();
		notifyDataSetChanged();
	}

	public void appendData(List<ResourceStatusInfo> array) {
		for(int index = 0; index < array.size(); index++){
			mResourceInfoItemList.add(array.get(index));
		}
		notifyDataSetChanged();
	}
}
